package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.BonusRuleInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyPointRuleActivity extends BaseActivity {


    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_scale)
    TextView tvScale;
    @BindView(R.id.tv_single_get_point_limit)
    TextView tvSingleGetPointLimit;
    @BindView(R.id.tv_today_get_point_limit)
    TextView tvTodayGetPointLimit;
    @BindView(R.id.tv_day_get_point_count)
    TextView tvDayGetPointCount;
    @BindView(R.id.tv_enter_point_time)
    TextView tvEnterPointTime;
    @BindView(R.id.tv_point_get_time)
    TextView tvPointGetTime;
    @BindView(R.id.tv_single_use_point_limit)
    TextView tvSingleUsePointLimit;
    @BindView(R.id.tv_single_use_point_max)
    TextView tvSingleUsePointMax;
    @BindView(R.id.tv_day_user_point_count_max)
    TextView tvDayUserPointCountMax;
    @BindView(R.id.tv_day_user_point_max)
    TextView tvDayUserPointMax;
    @BindView(R.id.tv_use_point_time)
    TextView tvUsePointTime;
    @BindView(R.id.tv_settle_rate)
    TextView tvSettleRate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_point_rule);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "积分规则");
        showView(R.id.next_sure, false);


        getdata();
    }


    private void getdata() {
        showProgress("加载中...");

        String url = Config.SBS_BONUS_RULE;
        RequestParams params = new RequestParams();
        params.put("groupId", MyApplication.getmInstance().getLoginMEntity().getGroupId() + "");
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

//                    JSONObject obj = result.getJSONObject("data");

                    BonusRuleInfo bonusRuleInfo = new Gson().fromJson(result.optString("data"), new TypeToken<BonusRuleInfo>() {
                    }.getType());

                    if (bonusRuleInfo != null) {
                        tvScale.setText("1元人民币 = " + bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_rate() + "积分");
                        tvSingleGetPointLimit.setText(bonusRuleInfo.getBonusSendRule_List().get(0).getSend_single_max() + "");
                        tvTodayGetPointLimit.setText(bonusRuleInfo.getBonusSendRule_List().get(0).getSend_day_max() + "");
                        tvDayGetPointCount.setText(bonusRuleInfo.getBonusSendRule_List().get(0).getSend_day_count() + "笔");

                        int accountTime = bonusRuleInfo.getBonusSendRule_List().get(0).getSend_accounted_time();
                        if (accountTime == 0) {
                            tvEnterPointTime.setText("实时入账");
                        } else {
                            tvEnterPointTime.setText(accountTime + "");
                        }
                        int timeType = bonusRuleInfo.getBonusSendRule_List().get(0).getSend_time_type();
                        if (timeType == 1) {
                            tvPointGetTime.setText("全天");
                        } else {
                            tvPointGetTime.setText(timeType + "");
                        }


                        if (bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_min_type() == 1) {
                            tvSingleUsePointLimit.setText(bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_min() + "");
                        } else {
                            tvSingleUsePointLimit.setText("消费金额*" + bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_min() + "%");
                        }
                        if (bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_max_type() == 1) {
                            tvSingleUsePointMax.setText(bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_max() + "");
                        } else {
                            tvSingleUsePointMax.setText("消费金额*" + bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_single_max() + "%");
                        }
                        tvDayUserPointCountMax.setText(bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_day_count() + "笔");
                        tvDayUserPointMax.setText(bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_day_sum() + "");
                        if (bonusRuleInfo.getBonusVerifyRule_List().get(0).getSettle_rate() != null) {
                            tvSettleRate.setText(bonusRuleInfo.getBonusVerifyRule_List().get(0).getSettle_rate() / 100 + "%");
                        }else{
                            tvSettleRate.setText("--");
                        }
                        int verifyTimeType = bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_time_type();
                        if (verifyTimeType == 1) {
                            tvUsePointTime.setText("全天");
                        } else {
                            String from = bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_time_from();
                            String to = bonusRuleInfo.getBonusVerifyRule_List().get(0).getVerify_time_to();
                            tvUsePointTime.setText(from + "~" + to);
                        }


                    }


                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


    @OnClick(R.id.titleback_btn_back)
    public void onViewClicked() {
        finish();
    }
}
